<template>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" :width="dialogInfo.width" align-center>
    <el-form ref="formRef" label-position="left"  :model="formModel" :rules="formRules" label-width="120px">
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker class="w100" v-model="formModel.startTime" type="date" placeholder="开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker class="w100" v-model="formModel.endTime" type="date" placeholder="结束时间"></el-date-picker>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info"  plain @click="onCancel()">取消</el-button>
        <el-button type="primary"  @click="onSubmit()">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>

import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import { getNewObj } from '@/utils/EsCommon';
// ======================================================
//                      变量声明
// ======================================================

// emit
const emit = defineEmits(['success']);
// 表单数据
const formAttrs = {
  id: "",
  startTime: "",
  endTime: "",
  sort: "",
};
const formModel = ref(Object.assign({}, formAttrs));
// 弹窗信息
const dialogInfo = ref({
  visible: false,
  title: '',
  width: '800px',
})
// 表单组件的引用实例
const formRef = ref(null);
// 加载状态

// 选项数据
// 分类ID树形选项数据
const optionsOfCategoryId = ref([]);
// 是否热门选项数据
const optionsOfIsHot = ref({});
// 是否推荐选项数据
const optionsOfIsRecommend = ref({});
// 状态选项数据
const optionsOfStatus = ref({});
// 表单校验规则
const formRules = ref({
  title: [{ required: true, trigger: "blur", message: "请输入学期名称" }],
  startTime: [{ required: true, trigger: "blur", message: "请选择开始时间" }],
  endTime: [{ required: true, trigger: "blur", message: "请选择结束时间" }],
})

// ======================================================
//                      点击操作
// ======================================================

// 点击提交按钮
const onSubmit = () => {
  console.log('[编辑表单] onSubmit formModel =', formModel.value);
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      console.log('表单校验失败：', fields);
      return;
    }
    let url = formModel.value.id > 0 ? ApiUrls.articleUpdate : ApiUrls.articleSave;
    EsAjax.post(url, formModel.value).then(res => {
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      dialogInfo.value.visible = false;
      emit('success');
    }).catch(err => {
      ElMessage.error('网络错误：' + err.message);
    })
  })
}


// 点击取消按钮
const onCancel = () => {
  formModel.value = Object.assign({}, formAttrs);
  formRef.value.resetFields();
  dialogInfo.value.visible = false;
}

// ======================================================
//                      处理事件
// ======================================================


// 加载数据
const show = (id, title, item = null, width = '600px') => {
  console.log(id, title, item, width)
  dialogInfo.value.title = title;
  dialogInfo.value.width = width;
  dialogInfo.value.visible = true;
  if (id) {

    formModel.value = Object.assign({ ...formAttrs }, getNewObj(item));
  }

}

defineExpose({
  show
})
</script>
